package com.charon.auth.demo;

import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @ClassName: DemoAuthController
 * @Description: 测试示例认证
 * @Author: Charon [248135724@qq.com]
 * @Date: 2019/1/12 0012 12:28
 * @Version: 1.0
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoAuthController {

    @Autowired
    private WebApplicationContext wac;


    private MockMvc mockMvc;

    /**
     * @param
     * @return void
     * @author Charon [248135724@qq.com]
     * @description 构建Mvc环境
     * @date 2019/1/12 0012 14:35
     */
    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void whenQuerySuccess() throws Exception {
        //模拟发送get请求
        mockMvc.perform(
                get("/user").param("userName", "mercury")
                        //设置请求类型
                        .contentType(MediaType.APPLICATION_JSON_UTF8))
                //判断返回值的状态码是否是200
                .andExpect(status().isOk())
                //判断返回的集合的长度是否是3
                .andExpect(MockMvcResultMatchers.
                        jsonPath("$.length()").value(3));

        //上面将两个类收藏后简写效果
        String content = mockMvc.perform(
                get("/user").param("size", "15")
                        .param("page", "3")
                        .param("sort", "age,desc")
                        .param("userName", "mercury")
                        .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.length()")
                        .value(3)).andReturn().getResponse().getContentAsString();
        log.info(content);
    }

    @Test
    public void whenGenInfoSuccess() throws Exception {
        String content = mockMvc.perform(get("/user/1").contentType(
                MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.userName")
                        .value("sun")).andReturn().getResponse().getContentAsString();
        log.info(content);
    }

    @Test
    public void whenGetInfoFail() throws Exception {
        mockMvc.perform(get("/user/a").contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().is4xxClientError());
    }

    @Test
    public void whenCreateSuccess() throws Exception {
        Date date=new Date();
        String content="{\"userName\":\"sun\",\"password\":null,\"birthday\":"+date.getTime()+"}";
        String string = mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(content)).andExpect(status().isOk()).andExpect(jsonPath("$.id").value("1")).andReturn().getResponse().getContentAsString();
        log.info(string);
    }

    @Test
    public void whenUpdateSuccess() throws Exception {
        //当前日期加一年
        Date date=new Date(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        log.info(date.getTime()+"");
        String content="{\"userName\":\"sun\",\"password\":\"mercury\",\"birthday\":"+date.getTime()+"}";
        String content1 = mockMvc.perform(put("/user/1").contentType(
                MediaType.APPLICATION_JSON_UTF8).content(content))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.userName")
                        .value("sun")).andReturn().getResponse().getContentAsString();
        log.info(content1);
    }

    @Test
    public void whenDeleteSuccess() throws Exception {
        mockMvc.perform(delete("/user/1").contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk());
    }

    //测试文件上传
    @Test
    public void whenUploadSuccess() throws Exception {
        mockMvc.perform(multipart("/file").file(new MockMultipartFile(
                "testFile","text.txt","multpart/form-data","hello upload".getBytes())))
                .andExpect(status().isOk());
    }
}
